
package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Param;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Controller
@RequestMapping("/qrcode")
public class QrcodeController {
    /**
     * 获取uuid及二维码图片地址
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/showQrcode")
    @ResponseBody
    protected JSONResult showQrGen(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        JSONResult jsonResult=new JSONResult();
        //生成UUID随机数
        UUID randomUUID = UUID.randomUUID();

        //通过应用获取共享的uuid集合
        Map<String, User> map = (Map) req.getServletContext().getAttribute("UUID_MAP");
        if (map == null) {
            map = new HashMap<String, User>();
            req.getServletContext().setAttribute("UUID_MAP", map);
        }
        //把uuid放入map中
       /* map.put(randomUUID.toString(), randomUUID.toString());*/

        //二维码图片扫描后的链接
        String url = Param.QRCODE_URL;
        url = url.replace("UUID",randomUUID.toString());
        Map resultMap=new HashMap();
        resultMap.put("qrurl",url);
        resultMap.put("uuid",randomUUID.toString());
        jsonResult.setResult(resultMap);
        return jsonResult;

    }

    /**
     * PC端不断进行轮询检查的方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/checkScan")
    @ResponseBody
    protected JSONResult checkScan(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        JSONResult jsonResult=new JSONResult();
        //获取页面的uuid参数
        String uuid = req.getParameter("uuid");
        //通过应用获取共享的uuid集合
        Map map = (Map) req.getServletContext().getAttribute("UUID_MAP");
        if (map != null) {
            //查询该uuid是否存在，且二维码已经被扫描并匹配到账号
            if(map.containsKey(uuid)){
                if (map.get(uuid) != null) {
                    User user = (User) map.get(uuid);
                    UserContext.setCurrentUser(user);
                    //从集合中移除
                    map.remove(uuid);
                    //设置登录信息
                    Map resultMap = new HashMap();
                    resultMap.put("uuid",uuid);
                    resultMap.put("user",user);
                    jsonResult.setResult(resultMap);
                   return jsonResult;
                }else{
                    return jsonResult;
                }
            }
        }
        return jsonResult;
    }

    @RequestMapping("/backToSimpleProfiles")
    public String backToSimpleProfiles(){
        return "/mine/simpleProfiles.html";
    }

}
